package com.leo.erp.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.erp.dao.MaterialPropertyMapper;
import com.leo.erp.dto.MaterialPropertyExecution;
import com.leo.erp.entity.MaterialProperty;
import com.leo.erp.entity.SystemConfig;
import com.leo.erp.enums.MaterialPropertyStateEnum;
import com.leo.erp.enums.SystemConfigStateEnum;
import com.leo.erp.service.LogService;
import com.leo.erp.service.MaterialPropertyService;
import com.leo.erp.util.GlobalRuntimeException;

@Service
public class MaterialPropertyServiceImpl implements MaterialPropertyService{
	@Autowired
	private MaterialPropertyMapper materialPropertyMapper;
	@Autowired
	private LogService logService;

	@Override
	public MaterialPropertyExecution updatematerialProperty(MaterialProperty materialProperty) {
		MaterialPropertyExecution me = new MaterialPropertyExecution();
		try {
			int effectedNum = materialPropertyMapper.updateById(materialProperty);
			if(effectedNum > 0) {
				logService.insertLog("商品属性", "修改" + materialProperty.getNativeName().toString(), ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
				me.setState(MaterialPropertyStateEnum.SUCCESS.getState());
			}else {
				me.setState(MaterialPropertyStateEnum.OFFLINE.getState());
			}
		} catch (GlobalRuntimeException e) {
			e.printStackTrace();
		}
		return me;
	}

	@Override
	public MaterialPropertyExecution getPageList(String nativeName, int currentPage, int pageSize) {
		MaterialPropertyExecution me = new MaterialPropertyExecution();
		//查询条件
		QueryWrapper<MaterialProperty> queryWrapper = new QueryWrapper<>();
		//判断
		if(nativeName != null && nativeName != "") {
			queryWrapper.like("nativeName", nativeName);
		}
		
		Page<MaterialProperty> page = new Page<>(currentPage, pageSize);//分页查询
		IPage<Map<String, Object>> iPage = materialPropertyMapper.selectMapsPage(page, queryWrapper);
		if(iPage.getRecords().size() > 0) {
			me.setState(MaterialPropertyStateEnum.SUCCESS.getState());
			me.setPages(page.getPages());//总页数
			me.setTotal(iPage.getTotal());//总记录数
			
			me.setMaterialPropertyPageList(iPage.getRecords());
		}else{
			me.setState(MaterialPropertyStateEnum.OFFLINE.getState());
		}
		
		return me;
	}
	
}
